package edu.centenary.centenaryController;
import java.io.*;
import java.net.*;
import java.util.*;

public class Connection extends CentController{
//Datamembers
private String ip;
private int port;
private Socket s;
private PrintWriter out;
private BufferedReader in;
private SensorData sd;
private Thread sg;
private Thread ai;
private Thread iw;
private boolean robotInitialized = false;
private String myName;
private ArrayList<WayPoint> wp;
private ImageWriter iw2;
private P2ATAI p2ai;
//Constructor
	public Connection(String ip, int port, String myName) throws UnknownHostException, IOException{
		this.ip = ip;
		this.port = port;
		this.s = new Socket(ip, port);
		this.out = new PrintWriter(s.getOutputStream(), true);
		this.in = new BufferedReader(new InputStreamReader(s.getInputStream()));
		this.sd = new SensorData();
		this.sg = new Thread(new SensorGather(sd, in, myName));
		this.sg.start();
		this.myName = myName;
		this.wp = new ArrayList<WayPoint>();

	}
//Methods
	public String getIp(){
		return ip;
	}
	public int getPort(){
		return port;
	}
	public void setPort(int port){
		this.port = port;
	}
	public Socket getSocket(){
		return s;
	}
	public PrintWriter getOut(){
		return out;
	}
	public BufferedReader getIn(){
		return in;
	}
	public SensorData getSd(){
		return sd;
	}
	public Thread getSg(){
		return sg;
	}

	public Thread getAi(){
		return ai;
	}
	public void setAi(Thread ai){
		this.ai = ai;
	}
	public Thread getIw(){
		return iw;
	}
	public void setIw(Thread iw){
		this.iw = iw;
	}
	public boolean getRobotInit(){
		return robotInitialized;
	}
	public void setRobotInit(boolean robotInitialized){
		this.robotInitialized = robotInitialized;
	}
	public String getMyName(){
		return myName;
	}
	public void setMyName(String myName){
		this.myName = myName;
	}
	public ArrayList<WayPoint> getWayPoint(){
		return wp;
	}
	public void setWayPoint(ArrayList<WayPoint> wp){
		this.wp = wp;
	}
	public void iwSetUp(){
		if(getRobotInit() && iw == null) {
					 
					 	System.out.println("******In the if .getRobotInit");
					 	iw2 = new ImageWriter(sd, wp, getMyName());
					 	iw = new Thread(iw2);
                  iw.start();
                }
	}
	public void aiStart(){
		p2ai = new P2ATAI(iw2,out, sd, wp);
		ai = new Thread(p2ai);
      ai.start();
		currentCon.setAi(ai);
	}
	public void closeConnections()throws IOException{
		  
        if (ai != null) {
		  		p2ai.writeBodyParts();
            ai.interrupt();
            //System.out.println("Interrupting AI thread");
            try {
                ai.join();
            } catch (InterruptedException e) {
            }
        }
        if (sg != null) {
            sg.interrupt();
            //System.out.println("Interrupting SensorGather thread");
            try {
                sg.join();
            } catch (InterruptedException e) {
            }
        }
        if (iw != null) {
            iw.interrupt();
            //System.out.println("Interrupting Image Writer thread");
            try {
                iw.join();
            } catch (InterruptedException e) {

            }
        }
        
            //System.out.println("Joining SG thread");
            out.close();
            //System.out.println("Closing out");
            in.close();
            
            //System.out.println("Closing stdIn");
            centSocket.close();

	}
}